package com.github.lzyzsd.circleprogress.utils.properties;

import com.github.lzyzsd.circleprogress.utils.properties.IInterface.AttributeObserver;
import com.github.lzyzsd.circleprogress.utils.properties.IInterface.IAttributeGetHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.solidxml.TypedAttribute;

public class AttrHelper implements IAttributeGetHelper {

    private Context mContext;
    private AttrSet mAttrSet;

    public AttrHelper(Context context, AttrSet attrSet) {
        this.mContext = context;
        this.mAttrSet = attrSet;
    }

    @Override
    public <T> void getTypeAttribute(String attrName, int type, AttributeObserver attributeObserver) {

        if( attributeObserver != null){
            if(mAttrSet == null || !mAttrSet.getAttr(attrName).isPresent()){
                return;
            }

            mAttrSet.getAttr(attrName).ifPresent(typedAttribute -> {

                switch (type) {
                    case TypedAttribute.BOOLEAN_ATTR:
                        attributeObserver.onGetAttribute(typedAttribute.getBoolValue());
                        return;
                    case TypedAttribute.COLOR_ATTR:
                        String colorHex = typedAttribute.getStringValue().replace("#", "");
                        Color result = new Color((int) Long.parseLong(colorHex, 16));
                        attributeObserver.onGetAttribute(result);
                        //attributeObserver.onGetAttribute(new Color(typedAttribute.getColorValue()));
                        return;
                    case TypedAttribute.FLOAT_ATTR:
                        attributeObserver.onGetAttribute(typedAttribute.getDimensionValue());
                        return;
                    case TypedAttribute.INTEGER_ATTR:
                        attributeObserver.onGetAttribute(typedAttribute.getIntegerValue());
                        return;
                    case TypedAttribute.STRING_ATTR:
                        attributeObserver.onGetAttribute(typedAttribute.getStringValue());
                        return;
                    case TypedAttribute.MEDIA_ATTR:
                        attributeObserver.onGetAttribute(typedAttribute.getElement());
                        return;
                    case TypedAttribute.LAYOUT_ATTR:
                        //attributeObserver.onGetAttribute(typedAttribute.getLayoutValue());
                        throw new RuntimeException("NOT SUPPORT");
                    case TypedAttribute.PATTERN_ATTR:
                        throw new RuntimeException("NOT SUPPORT");
                        //attributeObserver.onGetAttribute(typedAttribute.getPatternValue());
                    case TypedAttribute.GRAPHIC_ATTR:
                        attributeObserver.onGetAttribute(null);
                        return;
                }


            });

        }

    }
}
